﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DI;
using Front.Controllers.Api;
using Core.Dtos;
using Core.Messages;
using StructureMap;
using Core.Queries;
using StructureMap.AutoMocking;
using System.Transactions;
using Tests.Helpers;
using StructureMap.Pipeline;
using Front.Areas.Admin.Controllers.Api;
using CMS.Repositories;

namespace Insurance.Test
{
    [TestClass]
    public class DictionaryItemApiControllerTest
    {
        private TransactionScope transScope;
        private ControllerHelper<DictionaryItemApiController> controllerHelper;
        private ControllerHelper<DictionaryApiController> dictionaryControllerHelper;

        [ClassInitialize()]
        void Initialize()
        {            
        }

        [TestInitialize()]
        public void InitializeTest()
        {
            Bootstrapper.Init(new SingletonLifecycle());
            CurrentContextHelper.Logon();
            transScope = new TransactionScope(TransactionScopeOption.Required);

            dictionaryControllerHelper = new ControllerHelper<DictionaryApiController>();
            dictionaryControllerHelper.Initialize();            

            controllerHelper = new ControllerHelper<DictionaryItemApiController>();
            controllerHelper.Initialize();
        }

        [TestCleanup()]
        public void CleanupTest()
        {
            controllerHelper.Cleanup();
            Bootstrapper.Resolve<AdminDbContext>().Dispose();
            transScope.Dispose();            
        }

        int CreateDictionary()
        {
            var dictionaryController = dictionaryControllerHelper.GetController();                        
            var response = dictionaryController.Post(new DictionaryDto() { Name = "Dictionary1" });
            return response.Value;
        }

        [TestMethod]
        public void DictionaryItemApiController_CanCreate()
        {
            // Arrange        
            int dictionaryId = CreateDictionary();
            DictionaryItemDto itemDto = CreateSampleDto(dictionaryId);
            DictionaryItemApiController controller = controllerHelper.GetController();

            // Act            
            var response = controller.Post(dictionaryId, itemDto);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Create Item failed");            
        }

        [TestMethod]
        public void DictionaryItemApiController_CanRead()
        {
            // Arrange      
            int dictionaryId = CreateDictionary();
            DictionaryItemDto itemDto = CreateSampleDto(dictionaryId);
            DictionaryItemApiController controller = controllerHelper.GetController();
            var createResponse = controller.Post(dictionaryId, itemDto);
            var itemId = createResponse.Value;  

            // Act            
            var dto = controller.Get(dictionaryId, itemId);

            // Assert
            Assert.IsNotNull(dto, "Get Item failed");
        }

        [TestMethod]
        public void DictionaryItemApiController_CanEdit()
        {
            // Arrange     
            int dictionaryId = CreateDictionary();
            DictionaryItemDto itemDto = CreateSampleDto(dictionaryId);
            DictionaryItemApiController controller = controllerHelper.GetController();
            var createResponse = controller.Post(dictionaryId, itemDto);
            var itemId = createResponse.Value;            

            // Act            
            itemDto.Name = "Item2";
            var response = controller.Put(dictionaryId, itemId, itemDto);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Edit failed");
        }

        [TestMethod]
        public void DictionaryItemApiController_CanDelete()
        {
            // Arrange 
            int dictionaryId = CreateDictionary();
            DictionaryItemDto itemDto = CreateSampleDto(dictionaryId);
            DictionaryItemApiController controller = controllerHelper.GetController();
            var createResponse = controller.Post(dictionaryId, itemDto);
            var itemId = createResponse.Value;

            // Act
            var response = controller.Delete(dictionaryId, itemId);

            //Assert            
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Delete Item failed");
        }

        public DictionaryItemDto CreateSampleDto(int dictionaryId)
        {
            return new DictionaryItemDto() { Name = "Item1", DictionaryID = dictionaryId };
        }
    }
}
